package com.xhblogs.book.controller;

import com.xhblogs.book.pojo.Category;
import com.xhblogs.book.service.CategoryService;
import com.xhblogs.book.utils.ResultUtils;
import com.xhblogs.book.utils.ResultVo;
import com.xhblogs.book.utils.StatusCode;
import com.xhblogs.book.utils.TreePojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/api/v1/category")
public class CategoryContoller {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private TreePojo treePojo;

    @GetMapping("/list")
    public ResultVo getAll(){
        List<Category> allCategory = categoryService.getAllCategory();
        return ResultUtils.success("获取成功", StatusCode.SUCCESS_CODE,allCategory);
    }
    @GetMapping("/tree")
    public TreePojo getTree() {
        List<Map<String,Object>> category_List = new ArrayList<>();
        List<Category> allCategory = categoryService.getAllCategory();
        for (Category a: allCategory) {
            Map<String,Object> category = new HashMap<>();
            category.put("label",a.getCategoryName());
            category.put("value",a.getCategoryId());
            category_List.add(category);
        }
        treePojo.setLabel("图书类型");
        treePojo.setChildren(category_List);
        System.out.println(treePojo + "treePojo");
        return treePojo;
    }

    @GetMapping("/listCategory")
    public ResultVo getCategory(){
        List<Map<String, Object>> allCategory = categoryService.getCategory();
        return ResultUtils.success("获取成功", StatusCode.SUCCESS_CODE,allCategory);
    }
}
